package com.oriental.manage.controller.payment;

import com.alibaba.dubbo.config.annotation.Reference;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.pojo.payment.OutlineOrder;
import com.oriental.manage.service.payment.IOutlineOrderService;
import com.oriental.paycenter.external.api.operations.PayFacade;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by aaa on 2017/5/17.
 */
@Controller
@RequestMapping("payment/outlineOrder")
public class OutlineOrderController {

    @Autowired
    private IOutlineOrderService outlineOrderService;

    @RequestMapping("init")
    @RequiresPermissions("outline-order_search")
    public String init() {
        return "payment/searchOutlineOrderList";
    }

    @RequestMapping("search")
    @ResponseBody
    @RequiresPermissions("outline-order_search")
    public ResponseDTO search(@RequestBody Pagination<OutlineOrder> pagination, ResponseDTO<Pagination<OutlineOrder>> responseDTO) {
        outlineOrderService.selectSelective(pagination);

        responseDTO.setSuccess(true);
        responseDTO.setObject(pagination);

        return responseDTO;
    }

    @RequestMapping("toAdd")
    public String toAdd() {
        return "payment/editableOutlineOrder";
    }

    @RequestMapping("add")
    @ResponseBody
    @RequiresPermissions("outline-order_add")
    public ResponseDTO add(@RequestBody OutlineOrder outlineOrder, ResponseDTO responseDTO) {
        boolean res = outlineOrderService.post(outlineOrder);

        if (res) {
            responseDTO.setSuccess(true);
        } else {
            responseDTO.setSuccess(false);
            responseDTO.setMsg("新增失败,请确认参数合法性!");
        }

        return responseDTO;
    }

    @RequestMapping("update")
    @ResponseBody
    @RequiresPermissions("outline-order_update")
    public ResponseDTO update(@RequestBody OutlineOrder outlineOrder, ResponseDTO responseDTO) {

        if (outlineOrderService.put(outlineOrder)) {
            responseDTO.setSuccess(true);
        } else {
            responseDTO.setSuccess(false);
            responseDTO.setMsg("修改失败，请确认参数合法性!");
        }

        return responseDTO;
    }

    @RequestMapping("audit")
    @ResponseBody
    @RequiresPermissions("outline-order_audit")
    public ResponseDTO audit(@RequestBody OutlineOrder outlineOrder, ResponseDTO responseDTO) {

        outlineOrder.setStatus("02");
        if (outlineOrderService.put(outlineOrder)) {
            outlineOrderService.sendTransaction(outlineOrder);
            responseDTO.setSuccess(true);
        } else {
            responseDTO.setSuccess(false);
            responseDTO.setMsg("操作失败!");
        }

        return responseDTO;
    }

    @RequestMapping("discard")
    @ResponseBody
    @RequiresPermissions("outline-order_audit")
    public ResponseDTO discard(@RequestBody OutlineOrder outlineOrder, ResponseDTO responseDTO) {
        outlineOrder.setStatus("05");
        if (outlineOrderService.put(outlineOrder)) {
            responseDTO.setSuccess(true);
        } else {
            responseDTO.setSuccess(false);
            responseDTO.setMsg("操作失败!");
        }

        return responseDTO;
    }
}
